package cn.gailvlun.gll.presentation.emotion.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.util.DensityUtil;



public class EmotionDetailDecoration extends RecyclerView.ItemDecoration {
    protected Paint paint;
    private int maxSpace;
    private int minSpace;

    public EmotionDetailDecoration(Context context) {
        maxSpace = DensityUtil.dp2px(context, 8);
        minSpace = DensityUtil.dp2px(context, 0.5f);
        paint = new Paint();
        paint.setColor(context.getResources().getColor(R.color.line));
    }


    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int itemPosition = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition();
        int count = parent.getAdapter().getItemCount();
        if (count - 1 == itemPosition) {
            outRect.bottom = 0;
        } else {
            outRect.bottom = (itemPosition != 0) ? minSpace : maxSpace;
        }
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        int childCount = parent.getChildCount();
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();
        for (int i = 0; i < childCount - 1; i++) {
            View view = parent.getChildAt(i);
            float top = view.getBottom();
            float bottom = view.getBottom() + ((i != 0) ? minSpace : maxSpace);
            c.drawRect(left, top, right, bottom, paint);
        }
    }

}
